package co.wangdao.xingshaoye.view.activity;


import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import co.wangdao.xingshaoye.R;
import co.wangdao.xingshaoye.bean.Goods;
import co.wangdao.xingshaoye.bean.ShopGoods;
import co.wangdao.xingshaoye.bean.ShopMessage;
import co.wangdao.xingshaoye.utils.CommonAdapter;
import co.wangdao.xingshaoye.utils.ViewHolder;

/**
 * Created by Administrator on 2016/3/1 0001.
 */
public class CkdetailsActivity extends AppCompatActivity implements OnClickListener {
    private Button[] buttons;
    private View view;
    private LinearLayout ll;
    private List<Goods> goodsList;
    private List<Goods> goodsList3;
    private List<ShopGoods> shopGoodsList;
    private ShopMessage shopMessage;
    private List<ShopMessage> shopMessageList;
    private GridView gridViewgoods;
    private LinearLayout linearLayoutback;//返回按钮
    private Button btnguanzhu;
    private int state = 0; //按钮状态

    private static int STATUS_GUANZHU = 0;
    private static int STATUS_QUXIAOGUANZHU = 1;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ckdetailed);
        Intent intent = getIntent();
        String uid = intent.getStringExtra("uid");

        shopMessageList = new ArrayList<ShopMessage>();

        //实例化界面内容
        initView();

        //实例化测试数据
        initGoodsList();

        //实例化菜品类型选择
        initfoodtable();

        //设置适配器
        gridViewgoods = (GridView) findViewById(R.id.mealview);

        //初始化以及重新设置gridview数据

        setGridViewData(goodsList);

    }

    private void initView() {
        ll = (LinearLayout) findViewById(R.id.foodbutton);
        linearLayoutback = (LinearLayout) findViewById(R.id.backspace);
        linearLayoutback.setOnClickListener(this);
        btnguanzhu = (Button) findViewById(R.id.btnguanzhu);
        btnguanzhu.setOnClickListener(this);

    }

    private void setGridViewData(List list){
        gridViewgoods.setAdapter(new CommonAdapter<Goods>(getApplicationContext(), goodsList, R.layout.layout_meal_item) {
            @Override
            public void convert(ViewHolder helper, Goods item) {
                helper.setText(R.id.foodname, item.getTitle());
            }

        });
    }

    //实例化菜品类型选择
    private void initfoodtable() {


        int buttonIndex = shopGoodsList.size();

        for (int i = 0 ; i < buttonIndex ; i ++){
            ShopGoods good = shopGoodsList.get(i);
            final Button btn = new Button(this);
            btn.setTag(good.getId());
            btn.setText(good.getTitle());
            btn.setTextSize(16);
            btn.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
            btn.setBackgroundColor(getResources().getColor(R.color.colorWhite));
            if (i == 0){
                btn.setTextColor(getResources().getColor(R.color.yellow));
            }else{
                btn.setTextColor(getResources().getColor(R.color.gray));
            }

            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (int i = 0 ; i < ll.getChildCount() ; i++){
                        Button button = (Button) ll.getChildAt(i);
                        initBtnClickColor(button);
                    }
                    btn.setTextColor(getResources().getColor(R.color.yellow));
                    switch ((String)btn.getTag()){
                        case "2" :
                            goodsList = shopGoodsList.get(0).getGoodslist();
                            break;
                        case "3" :
                            goodsList = shopGoodsList.get(1).getGoodslist();
                            break;
                        case "4" :
                            goodsList = shopGoodsList.get(2).getGoodslist();
                            break;
                    }
                    if(goodsList != null){
                        findViewById(R.id.wushuju).setVisibility(View.INVISIBLE);
                        gridViewgoods.setVisibility(View.VISIBLE);
                        setGridViewData(goodsList);
                    }else{
                        findViewById(R.id.wushuju).setVisibility(View.VISIBLE);
                        gridViewgoods.setVisibility(View.INVISIBLE);
                    }
                }
            });
            ll.addView(btn);
        }

    }

    /**
     * 初始化按钮点击状态
     */
    private void initBtnClickColor(Button btn){
        btn.setTextColor(getResources().getColor(R.color.gray));
    }

    /**
    * 初始化数据
    */
    private void initGoodsList(){
        shopMessage = new ShopMessage();
        ShopGoods shopGoods1 = new ShopGoods();
        ShopGoods shopGoods2 = new ShopGoods();
        ShopGoods shopGoods3 = new ShopGoods();
        shopGoodsList = new ArrayList<ShopGoods>();
        Goods goods1 = new Goods();
        Goods goods2 = new Goods();
        Goods goods3 = new Goods();
        goodsList = new ArrayList<Goods>();
        goodsList3 = new ArrayList<Goods>();

        goods1.setId("48");
        goods1.setTitle("牛肉面");
        goods1.setImg("http://web1.app.ijiaque.com/Uploads/Picture/2016-01-29/56ab10449dacb_thumb_360_360.jpg");
        goods1.setBrief("牛肉面牛肉面牛肉面");
        goods1.setPrice("20.00");
        goods1.setMarket_price("25");
        goods1.setSell_num("0");

        goodsList.add(goods1);

        goods2.setId("49");
        goods2.setTitle("牛肉面49");
        goods2.setImg("http://web1.app.ijiaque.com/Uploads/Picture/2016-01-29/56ab10449dacb_thumb_360_360.jpg");
        goods2.setBrief("牛肉面牛肉面牛肉面...");
        goods2.setPrice("20.00");
        goods2.setMarket_price("25");
        goods2.setSell_num("0");

        goodsList.add(goods2);

        goods3.setId("50");
        goods3.setTitle("牛肉面50");
        goods3.setImg("http://web1.app.ijiaque.com/Uploads/Picture/2016-01-29/56ab10449dacb_thumb_360_360.jpg");
        goods3.setBrief("牛肉面牛肉面牛肉面...");
        goods3.setPrice("20.00");
        goods3.setMarket_price("25");
        goods3.setSell_num("0");

        goodsList.add(goods3);

        shopGoods1.setId("2");
        shopGoods1.setTitle("单品");
        shopGoods1.setGoodslist(goodsList);

        shopGoods2.setId("3");
        shopGoods2.setTitle("套餐");

        goodsList3.add(goods1);
        goodsList3.add(goods2);
        shopGoods3.setId("4");
        shopGoods3.setTitle("饮品");
        shopGoods3.setGoodslist(goodsList3);


        shopGoodsList.add(shopGoods1);
        shopGoodsList.add(shopGoods2);
        shopGoodsList.add(shopGoods3);


        shopMessage.setList(shopGoodsList);
        //店铺详细信息
        shopMessageList.add(shopMessage);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.backspace: onBackPressed();
                break;

            case R.id.btnguanzhu:switch (state) {
                case 0:
                   // btnguanzhu.setBackground(this.getResources().getDrawable(R.drawable.guanzhu));
                    btnguanzhu.setBackgroundResource(R.drawable.quxiaoguanzhu);
                    state = STATUS_QUXIAOGUANZHU;
                    break;
                case 1:
                    btnguanzhu.setBackgroundResource(R.drawable.guanzhu);
                    state = STATUS_GUANZHU;
                    break;
            }
        }

    }
}
